import datetime
import random


def shopping_center_1():
    money = 4000  # 账户余额
    consume = 0  # 消费额
    commodity = {  # 商品列表
        "鼠标": 319,
        "键盘": 679,
        "耳机": 399,
        "显示器": 799,
        "U盘": 168,
        "电源适配器": 150
    }
    cart = {}  # 购物车

    print("=== 欢迎光临购物中心！ ===")
    print("=== 尽情选购！ ===")
    print("-" * 30)
    print(f"商品列表：")
    for item, price in commodity.items():
        print(f"{item}: ￥{price}")

    while True:
        choice = input("输入商品名称加购（或者按Q结算退出）：")
        if choice == 'Q' or choice == 'q':
            print("-" * 30)
            print("您的账单如下：")
            print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
            for item, price in cart.items():
                print(f"{item}: ￥{price}")
            print(f"您一共消费了{consume}元，账户余额{money}元！")
            print("谢谢惠顾！")
            break
        elif commodity.get(choice):
            if money > commodity.get(choice):
                money -= commodity.get(choice)
                consume += commodity.get(choice)
                cart[choice] = commodity.get(choice)
                print(f"加购成功！账户余额：{money}元")
            elif money < commodity.get(choice):
                print("穷鬼，钱不够！请到其他超市购买！")
        else:
            print("没有这个商品，别瞎弄！")


def shopping_center_2():
    money = 4000  # 账户余额
    consume = 0  # 消费额
    commodity = {  # 商品列表
        "鼠标": 319,
        "键盘": 679,
        "耳机": 399,
        "显示器": 799,
        "U盘": 168,
        "电源适配器": 150,
        "空调": 2000,  # 新增商品，用于演示优惠券
        "冰箱": 1500,
        "洗衣机": 1200,
        "电视": 3000
    }
    cart = {}  # 购物车

    print("=== 欢迎光临购物中心！ ===")

    # 抽取优惠券
    print("=== 先来抽取一张优惠券吧！ ===")
    discount_items = ["空调", "冰箱", "洗衣机", "电视", "显示器", "耳机", "键盘", "鼠标", "U盘", "电源适配器"]
    discount_rates = [0.8, 0.85, 0.9, 0.75, 0.7, 0.8, 0.85, 0.9, 0.95, 0.8]

    input("按回车抽取优惠券...")
    # 随机抽取一张优惠券
    discount_item = random.choice(discount_items)
    discount_rate = random.choice(discount_rates)

    print(f"恭喜您！抽到了【{discount_item}】的{int(discount_rate * 10)}折优惠券！")
    print(f"购买{discount_item}时，只需支付原价{commodity[discount_item]}元的{int(discount_rate * 100)}%！")

    input("按回车开始选购...")

    print("\n=== 尽情选购！ ===")
    print("-" * 30)
    print(f"商品列表：")
    for item, price in commodity.items():
        if item == discount_item:
            discount_price = int(price * discount_rate)
            print(f"{item}: 原价￥{price} → 优惠价￥{discount_price} (您有{int(discount_rate * 10)}折券)")
        else:
            print(f"{item}: ￥{price}")

    while True:
        print("\n当前操作选项：")
        print("1. 输入商品名称 - 加购商品")
        print("2. 输入'退+商品名称' - 退货（例如：退空调）")
        print("3. 输入'Q' - 结算退出")

        choice = input("请选择操作：")

        # 结算退出
        if choice == 'Q' or choice == 'q':
            print("-" * 30)
            print("您的账单如下：")
            print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
            for item, price in cart.items():
                print(f"{item}: ￥{price}")
            print(f"您一共消费了{consume}元，账户余额{money}元！")
            print("谢谢惠顾！")
            break

        # 退货功能
        elif choice.startswith('退'):
            item_to_return = choice[1:]  # 去掉"退"字
            if item_to_return in cart:
                return_price = cart[item_to_return]
                money += return_price
                consume -= return_price
                del cart[item_to_return]
                print(f"成功退回【{item_to_return}】，退款{return_price}元！")
                print(f"当前账户余额：{money}元，消费总额：{consume}元")
                print(f"购物车：{list(cart.keys())}")
            else:
                print(f"购物车中没有【{item_to_return}】商品！")

        # 购买商品
        elif commodity.get(choice):
            # 计算商品价格（考虑优惠券）
            if choice == discount_item:
                item_price = int(commodity[choice] * discount_rate)
                print(f"使用优惠券！{choice}享受{int(discount_rate * 10)}折优惠！")
            else:
                item_price = commodity.get(choice)

            if money >= item_price:
                money -= item_price
                consume += item_price
                cart[choice] = item_price
                print(f"加购成功！【{choice}】花费{item_price}元")
                print(f"账户余额：{money}元")
                print(f"购物车：{list(cart.keys())}")
            else:
                print("穷鬼，钱不够了！请到其他超市购买！")

        else:
            print("没有这个商品，请重新输入！")

if __name__ == '__main__':
    # 运行购物系统1.0
    # shopping_center_1()
    # 运行购物系统2.0
    shopping_center_2()
